Trying to sentinel loop this program. [java]
Posted
by roger34
on Stack Overflow
See other posts from Stack Overflow
or by roger34
Published on 2010-06-07T00:58:37Z
Indexed on
2010/06/07
1:02 UTC
Read the original article
Hit count: 243
Okay, I spent all this time making this for class but I have one thing that I can't quite get: I need this to sentinel loop continuously (exiting upon entering x) so that the
System.out.println("What type of Employee? Enter 'o' for Office " +
"Clerical, 'f' for Factory, or 's' for Saleperson. Enter 'x' to exit." );
line comes back up after they enter the first round of information. Also, I can't leave this up long on the (very) off chance a classmate might see this and steal the code. Full code following:
import java.util.Scanner;
public class Project1 {
public static void main (String args[]){
Scanner inp = new Scanner( System.in );
double totalPay;
System.out.println("What type of Employee? Enter 'o' for Office " +
"Clerical, 'f' for Factory, or 's' for Saleperson. Enter 'x' to exit." );
String response= inp.nextLine();
while (!response.toLowerCase().equals("o")&&!response.toLowerCase().equals("f")
&&!response.toLowerCase().equals("s")&&!response.toLowerCase().equals("x")){
System.out.print("\nInvalid selection,please enter your choice again:\n");
response=inp.nextLine();
}
char choice = response.toLowerCase().charAt( 0 );
switch (choice){
case 'o':
System.out.println("Enter your hourly rate:");
double officeRate=inp.nextDouble();
System.out.println("Enter the number of hours worked:");
double officeHours=inp.nextDouble();
totalPay = officeCalc(officeRate,officeHours);
taxCalc(totalPay);
break;
case 'f':
System.out.println("How many Widgets did you produce during the week?");
double widgets=inp.nextDouble();
totalPay=factoryCalc(widgets);
taxCalc(totalPay);
break;
case 's':
System.out.println("What were your total sales for the week?");
double totalSales=inp.nextDouble();
totalPay=salesCalc(totalSales);
taxCalc(totalPay);
break;
}
}
public static double taxCalc(double totalPay){
double federal=totalPay*.22;
double state =totalPay*.055;
double netPay = totalPay - federal - state;
federal =federal*Math.pow(10,2);
federal =Math.round(federal);
federal= federal/Math.pow(10,2);
state =state*Math.pow(10,2);
state =Math.round(state);
state= state/Math.pow(10,2);
totalPay =totalPay*Math.pow(10,2);
totalPay =Math.round(totalPay);
totalPay= totalPay/Math.pow(10,2);
netPay =netPay*Math.pow(10,2);
netPay =Math.round(netPay);
netPay= netPay/Math.pow(10,2);
System.out.printf("\nTotal Pay \t: %1$.2f.\n", totalPay);
System.out.printf("State W/H \t: %1$.2f.\n", state);
System.out.printf("Federal W/H : %1$.2f.\n", federal);
System.out.printf("Net Pay \t: %1$.2f.\n", netPay);
return totalPay;
}
public static double officeCalc(double officeRate,double officeHours){
double overtime=0;
if (officeHours>=40)
overtime = officeHours-40;
else
overtime = 0;
if (officeHours >= 40)
officeHours = 40;
double otRate = officeRate * 1.5;
double totalPay= (officeRate * officeHours) + (otRate*overtime);
return totalPay;
} public static double factoryCalc(double widgets){
double totalPay=widgets*.35 +300;
return totalPay;
}
public static double salesCalc(double totalSales){
double totalPay = totalSales * .05 + 500;
return totalPay;
} }
© Stack Overflow or respective owner